package net.mysoft.framework.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 
 * @author liuyang
 * @see 该类作为系统日志输出类
 */
public class Log4jAOP {
	// private static Logger logger = Logger.getLogger(Log4jAOP.class);
	private static Log logger = LogFactory.getLog(Log4jAOP.class);

	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		String strMethod = "";
		String strClassName = "";
		String strMethodName = "";
		long lgWarningTime = 5;
		Object[] oArray = joinPoint.getArgs();

		strClassName = joinPoint.getTarget().getClass().getName();
		strMethodName = joinPoint.getSignature().getName();
		if (oArray.length > 0) {
			for (int i = 0; i < oArray.length; i++) {
				if (oArray[i] != null) {
					strMethod = strMethod
							+ oArray[i].getClass().getSimpleName() + ": "
							+ oArray[i].toString() + ",";
				}
			}
			if (strMethod != null && !"".equals(strMethod)
					&& strMethod.length() > 0)
				strMethod = strMethod.substring(0, strMethod.length() - 1);
		}
		logger.warn("Method " + strClassName + "." + strMethodName + "("
				+ strMethod + ") is called!");

//		long lgStartTime = System.currentTimeMillis();
		// 调用主方法
		Object result = joinPoint.proceed();
//		long lgEndTime = System.currentTimeMillis();
//		long lgTime = (lgEndTime - lgStartTime) / 1000;
//
//		if (result != null) {
//			logger.warn("Method " + strClassName + "." + strMethodName
//					+ " result is [" + result.getClass().getSimpleName() + ": "
//					+ result + "]");
//		} else {
//			logger.warn("Method " + strClassName + "." + strMethodName
//					+ " result is void!");
//		}
//		if (lgTime > lgWarningTime) {
//			logger.warn("Method cost times: " + lgTime + "s");
//		}

		return result;

		// try {
		// } catch (Exception e) {
		// e.printStackTrace();
		// logger.warn(joinPoint.getTarget().getClass() + "."
		// + joinPoint.getSignature().getName() + "() around error");
		// logger.warn("error info [" + e.getMessage() + "]");
		// } finally {
		// //logger.warn("Ending method : " + joinPoint.getTarget().getClass()
		// // + "." + joinPoint.getSignature().getName() + "()");
		// logger.warn(joinPoint.getTarget().getClass()
		// + "." + joinPoint.getSignature().getName() + "()"+
		// "方法运行时间: "
		// + (System.currentTimeMillis() - startTime) + " ms.");
		// }
		// return null;

	}
}
